﻿using System;
using System.Data;
using PowerToolsWeb.DataAccess;

namespace PowerToolsWeb
{
    public partial class ProductList : System.Web.UI.Page
    {
        private string KeyWord
        {
            get
            {
                return Request.QueryString["keyword"] == null ? "" : Request.QueryString["keyword"].ToString().Trim();
            }
        }
        private int TypeId
        {
            get
            {
                string _typeId = "";
                _typeId = Request.QueryString["typeId"] == null ? "" : Request.QueryString["typeId"].ToString();
                int typeId = 0;
                int.TryParse(_typeId, out typeId);
                return typeId;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Tpt_ProductCollection product = new Tpt_ProductCollection();
                DataTable dt = new DataTable();
                if (TypeId != 0)
                {
                    product.ListProductForList(TypeId, "");
                }
                else if (KeyWord != "")
                {
                    product.ListProductForList(-1, FormatKeyName(KeyWord));
                }
                else
                {
                    product.ListProductForList(-1, "");
                }
                dt = product.DataTable;
                this.Repeater1.DataSource = dt;
                this.Repeater1.DataBind();
                if (dt.Rows.Count < 1)
                {
                    this.lblMesg.Text = "对不起！暂时没有相关的商品";
                    this.lblMesg.Visible = true;
                }
                else
                {
                    this.lblMesg.Visible = false;
                }
            }
        }
        //以%取代特殊字符
        public static string FormatKeyName(string _strText)
        {
            char[] _char = { '@', '"', '<', '>', '!', '*', '&', '\\', '/', '\'', '+', '-', '#', '^', '|', '?', ',', '$', ' ' };
            _strText = _strText.Trim();
            foreach (char c in _strText)
            {
                foreach (char _c in _char)
                {
                    if (c == _c)
                    {
                        _strText = _strText.Replace(c, '%');
                    }
                }
            }
            _strText = _strText.Trim(new char[] { '%' });
            return _strText;
        }
    }
}
